{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 完成Numpy的数组和原生python的性能对比",
   "id": "4d1cb9f6e860136c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T15:03:42.446052Z",
     "start_time": "2025-02-26T15:03:42.398335Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import time\n",
    "import random"
   ],
   "id": "e47a81e995e74edc",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-02-26T15:04:09.033681Z",
     "start_time": "2025-02-26T15:03:42.446052Z"
    }
   },
   "cell_type": "code",
   "source": [
    "my_list = []\n",
    "for i in range(1, 10 ** 8):\n",
    "    my_list.append(random.randint(1, 100))\n",
    "\n",
    "time_1 = time.time()\n",
    "sum(my_list)\n",
    "time_2 = time.time()\n",
    "print(f\"列表计算花费时间为：{time_2 - time_1}s\")\n",
    "time_5 = time.time()\n",
    "np_array = np.array(my_list)  # 转化\n",
    "time_6 = time.time()\n",
    "print(f\"列表转化为np的ndarray花费时间为：{time_6 - time_5}s\")\n",
    "time3 = time.time()\n",
    "np.sum(np_array)\n",
    "time4 = time.time()\n",
    "print(f\"np的计算花费时间为：{time4 - time3}\")"
   ],
   "id": "766b33d95932a2c8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表计算花费时间为：0.8232135772705078s\n",
      "列表转化为np的ndarray花费时间为：2.3990206718444824s\n",
      "np的计算花费时间为：0.03400278091430664\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
